import 'package:flutter/material.dart';
import 'package:terminal_flutter_demo/res/user_daata_list.dart';
import 'package:terminal_flutter_demo/widgets/my_swiper.dart';

class PageViewPage extends StatefulWidget {
  const PageViewPage({Key? key}) : super(key: key);

  @override
  _PageViewPageState createState() => _PageViewPageState();
}

class _PageViewPageState extends State<PageViewPage> {
  @override
  Widget build(BuildContext context) {
    return PageView(
      scrollDirection: Axis.vertical, // 滑动方向
      allowImplicitScrolling: true, // 缓存当前页面的前后两页
      onPageChanged: (index) {
        print('onPageChanged: $index');
      },
      // children: (() {
      //   return [1, 2, 3, 4, 5, 6]
      //       .map((index) => Center(
      //             child: Text('第 $index 页'),
      //           ))
      //       .toList();
      // })(),
      children: (() {
        return List.generate(
            6,
            (index) => Center(
                  child: Text('第 ${++index} 页'),
                )).toList();
      })(),
    );
  }
}

class PageViewBuildPage extends StatelessWidget {
  const PageViewBuildPage({super.key});

  @override
  Widget build(BuildContext context) {
    return PageView.builder(
      scrollDirection: Axis.vertical,
      itemCount: 6,
      itemBuilder: (context, index) {
        return Center(
          child: Text('PageView.builder 第 ${++index} 页'),
        );
      },
    );
  }
}

class SwiperPage extends StatefulWidget {
  const SwiperPage({super.key});

  @override
  State<SwiperPage> createState() => _SwiperPageState();
}

class _SwiperPageState extends State<SwiperPage> {
  @override
  void initState() {
    super.initState();
  }

  List<Widget> _getSwiperChildren() {
    List<Widget> list = [];
    var listData = userDataList.where((element) {
      if (element['index'] != null && element['index'] is int) {
        return element['index'] as int < 5;
      }
      return false;
    }).toList();
    listData.map((item) {
      list.add(Image.network(
        '${item['avatar_source']}',
        fit: BoxFit.cover,
      ));
    }).toList();
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return MySwiper(
      width: double.infinity,
      height: 220,
      scrollDirection: Axis.horizontal,
      swipers: _getSwiperChildren(),
    );
  }
}
